package com.lsa.iview.unsorted;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class Main {
	public static void main(String[] args) {
//		int n = 5;
//		for (int l = 1; l <= 3; l++) {
//			for (int i = 0; i < n - l + 1; i++) {
//				int j = l + i - 1;
//				System.out.print(String.format("(%s,%s)", i,j));
//			}
//			System.out.println();
//		}
		
		Tree t = toTree(Arrays.asList(1,2,3,4,5,6,7));
		System.out.println(t);
	}
	
	public static Tree toTree(List<Integer> data){

	      return toTree(data.iterator(), data.size());
	      
	}

	private static Tree toTree(Iterator<Integer> i, int size){
	      if (size == 0){
	            return null;
	      } else if (size == 1){
	            return new Tree(i.next());
	      } else {
	            Tree left = toTree(i, size/2);
	            Tree root = new Tree(i.next());
	            Tree right = toTree(i, size - size/2 -1);
	            
	            root.left = left;
	            root.right = right;
	            return root;
	      }

	}
	
	private static class Tree {
		public Tree(Integer d) {
			data = d;
		}

		Tree left;
		Tree right;
		
		int data;
	}

}
